package net.guruqu.rvoView;

import java.awt.Canvas;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import com.gibbon.jme.context.JmeContext;
import com.gibbon.jme.context.lwjgl.LWJGLCanvas;
import com.gibbon.jme.context.lwjgl.LWJGLContext;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.system.GameSettings;
import com.jme.system.PropertiesGameSettings;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.export.xml.XMLExporter;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;

public class ModelUtil {
	public static void convertFile(String original, String target) {
		File f = new File(original);
		byte[] buf = null;

		original = original.toUpperCase();
		FormatConverter converter = null;
		if (original.endsWith("3DS")) {
			converter = new MaxToJme();
		} else if (original.endsWith("OBJ")) {
			converter = new ObjToJme();
			try {
				converter.setProperty("mtllib", (new File(".")).toURL());
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		} else if (original.endsWith("ASE")) {
			converter = new AseToJme();
		} /*
			 * else if (original.endsWith("MD5")) { // converter = new }
			 */else
			throw new RuntimeException("Unknown fomrat:" + original);
		ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
		try {
			converter.convert(new FileInputStream(original), arrayOutputStream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		buf = arrayOutputStream.toByteArray();
		BinaryImporter binaryImporter = new BinaryImporter();
		Spatial node = null;
		try {
			node = (Spatial) binaryImporter.load(buf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		if (node instanceof Node) {
			((Node) node).setLightCombineMode(LightCombineMode.Off);
		}
		writeFile(node, target);
	}
	static GameSettings gs;
	static{
		gs = new PropertiesGameSettings("properties.cfg");
	}
	
	public static JmeContext createCanvas(int w, int h, JmeContext sharedContext) {
		JmeContext context = null;
		try {
			context = JmeContext.create(LWJGLContext.class,
					JmeContext.CONTEXT_CANVAS);
			context.setSettings(gs);
			if (sharedContext != null)
				context.setParentContext(sharedContext);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		context.start();
		Canvas canvas = context.getCanvas();
//		canvas.setIgnoreRepaint(true);
		(canvas).setSize(640, 480);
		return context;
	}

	static boolean checkNormal(float[] normals, int len) {
		boolean normal = true;
		for (int i = 0; i < len; i += 3) {
			float norm = normals[i] * normals[i] + normals[i + 1]
					* normals[i + 1] + normals[i + 2] * normals[i + 2];
			if (Math.abs(1f - norm) > 1e-3)
				normal = false;

		}
		return normal;
	}

	static Spatial getNode(Spatial root, int indices[], int depth) {

		if (depth == indices.length)
			return root;
		if (((Node) root).getChildren() == null)
			return null;
		Spatial node = ((Node) root).getChildren().get(indices[depth]);
		return getNode(node, indices, depth + 1);

	}

	static void writeFile(Savable n, String file) {
		try {
			XMLExporter exporter = new XMLExporter();
			// ByteInputStream byteInputStream = new ByteInputStream(b, 0);
			exporter.save(n, new File(file));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	static void printNodeStructure(Node node, int depth) {

		for (int i = 0; i < depth; i++)
			System.out.print("|");
		System.out.println("(" + node.getClass().getName() + ")"
				+ node.getName());

		List<Spatial> children = node.getChildren();
		if (children != null) {
			for (Spatial s : children) {
				if (s instanceof Node) {
					printNodeStructure((Node) s, depth + 1);
				} else {
					for (int i = 0; i < depth + 1; i++)
						System.out.print("|");
					System.out.println("(" + s.getClass().getName() + ")"
							+ s.getName());
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
